<template>
  <div class="topic">
    <div class="topic-container">
      <div class="topic-header">
        <div class="title">{{ datas.form.title }}</div>
        <div class="more">更多<i class="el-icon-arrow-right right"></i></div>
      </div>
      <div class="topic-body">
        <!-- 宫格 -->
        <div
          v-if="datas.form.arrange_type == 1"
          class="topic-body-list grid"
          :class="{
            'grid-columns-2': datas.form.column == 2,
            'grid-columns-3': datas.form.column == 3,
            'grid-columns-4': datas.form.column == 4
          }"
        >
          <div v-for="item in datas.mock_source" :key="item" class="item">
            <el-image class="image" :src="item.cover_url">
              <div slot="error" class="image-error">
                <img
                  class="image-error__icon"
                  src="@/assets/images/template/h5/placeholder_medium.png"
                />
              </div>
            </el-image>
            <div class="desc">
              <div class="title">{{ item.title }}</div>
              <div class="info">
                {{ item.intro }}
              </div>
              <div class="bottom">
                <span>{{ item.create_time }}</span>
                <span>
                  <i class="el-icon-view el-icon--left"></i>
                  {{ item.view }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <!-- 滚动 -->
        <div
          v-else-if="datas.form.arrange_type == 2"
          class="topic-body-list scroll"
        >
          <swiper :options="swiperOption">
            <swiper-slide v-for="item in datas.mock_source" :key="item">
              <div class="item">
                <el-image class="image" :src="item.cover_url">
                  <div slot="error" class="image-error">
                    <img
                      class="image-error__icon"
                      src="@/assets/images/template/h5/placeholder_medium.png"
                    />
                  </div>
                </el-image>
                <div class="desc">
                  <div class="title">{{ item.title }}</div>
                  <div class="info">
                    {{ item.intro }}
                  </div>
                  <div class="bottom">
                    <span>{{ item.create_time }}</span>
                    <span>
                      <i class="el-icon-view el-icon--left"></i>
                      {{ item.view }}
                    </span>
                  </div>
                </div>
              </div>
            </swiper-slide>
          </swiper>
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>
        </div>
      </div>
    </div>

    <!-- 删除组件 -->
    <slot name="deles" />
  </div>
</template>

<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
  name: 'Topic',
  components: {
    swiper,
    swiperSlide
  },
  props: {
    datas: {
      type: Object,
      default: () => {}
    },
    isDroping: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      swiperOption: {
        slidesPerView: 3,
        spaceBetween: 10,
        loop: false,
        loopAdditionalSlides: 0,
        lazy: true,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
      // sourceList: []
    }
  },
  watch: {
    'datas.form.source_index': {
      handler(val) {
        this.loadBatchInfo()
      }
    }
  },
  created() {
    if (this.isDroping === false) {
      this.loadBatchInfo()
    }
  },
  methods: {
    loadBatchInfo() {
      let sourceList = []
      if (this.datas.form.source === 0) {
        const ids = this.datas.form.source_list.map(item => item.value)
        if (ids.length > 0) {
          this.$http.get(
            '/topic/admin/batch/info',
            {
              ids: ids
            },
            ({ data }) => {
              this.datas.form.source_list.forEach(item => {
                const info = data.find(i => i.id === item.value)
                sourceList.push({
                  id: info.id,
                  title: info.title,
                  create_time: this.$util.formatTime(info.publish_time),
                  view: info.read_count,
                  cover_url: info.cover_url
                })
              })
            },
            _ => {}
          )
        }
      } else {
        sourceList = this.getMockData()
      }

      this.$set(this.datas, 'mock_source', sourceList)
    },
    // 模拟数据
    getMockData() {
      const mockData = []
      const topic = {
        title: '资讯标题',
        create_time: '5小时前',
        view: '99',
        intro: '这是资讯的简介示例内容',
        cover_url: ''
      }
      for (let i = 0; i < this.datas.form.number; i++) {
        const item = { ...topic }
        item.cover_url = require('@/assets/images/template/topic_' +
          ((i % 4) + 1) +
          '.png')
        mockData.push(item)
      }

      return mockData
    }
  }
}
</script>

<style scoped lang="scss">
.topic {
  position: relative;
  padding: 15px 0;
  &-container {
    width: 834px;
    margin: 0 auto;
    background: #ffffff;
  }
  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 62px;
    padding: 0 15px;
    .title {
      font-size: 17px;
      font-weight: 500;
      color: #303133;
      line-height: 24px;
    }
    .more {
      font-size: 12px;
      font-weight: 400;
      color: #888888;
      line-height: 16px;
    }
  }
  &-body {
    padding: 0 15px 20px;
    &-list {
      .item {
        min-width: 0;
        .image {
          display: block;
        }
        ::v-deep.image-error {
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
          background-color: #f1f1f1;
          .image-error__icon {
            width: 40px;
            height: 28px;
          }
        }
        .title {
          font-size: 12px;
          font-weight: 500;
          color: #333333;
          line-height: 16px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .info {
          font-size: 12px;
          font-weight: 400;
          color: #606266;
          line-height: 16px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .bottom {
          display: flex;
          align-items: center;
          justify-content: space-between;
          font-size: 12px;
          font-weight: 400;
          color: #b7bac1;
          line-height: 14px;
        }
      }
      &.grid {
        display: grid;
        grid-gap: 14px 10px;
        &.grid-columns-2 {
          grid-template-columns: repeat(2, 1fr);
          .item {
            display: flex;
            background: rgba(48, 49, 51, 0.02);
            border-radius: 6px;
            padding: 5px;
            .image {
              width: 170px;
              height: 90px;
            }
            .desc {
              margin-left: 8px;
              padding: 5px 0;
              min-width: 0;
              flex: 1;
            }
            .info {
              margin-top: 8px;
            }
            .bottom {
              margin-top: 10px;
            }
          }
        }
        &.grid-columns-3 {
          grid-template-columns: repeat(3, 1fr);
          .item {
            .image {
              height: 145px;
            }
            .desc {
              margin-top: 8px;
            }
            .info {
              margin-top: 8px;
            }
            .bottom {
              margin-top: 8px;
            }
          }
        }
        &.grid-columns-4 {
          grid-template-columns: repeat(4, 1fr);
          .item {
            .image {
              height: 108px;
            }
            .desc {
              margin-top: 8px;
            }
            .info {
              margin-top: 8px;
            }
            .bottom {
              margin-top: 8px;
            }
          }
        }
      }
      &.scroll {
        position: relative;
        padding: 0 10px;
        .swiper-button-prev,
        .swiper-button-next {
          outline: none;
          width: 20px;
          height: 20px;
          margin-top: -50px;
          color: #b7bac1;
          &:after {
            font-size: 16px;
          }
        }
        .swiper-button-prev.swiper-button-disabled,
        .swiper-button-next.swiper-button-disabled {
          pointer-events: auto;
        }
        .swiper-button-prev {
          left: -15px;
          right: auto;
        }
        .swiper-button-next {
          right: -15px;
          left: auto;
        }
        .item {
          .image {
            height: 145px;
          }
          .desc {
            margin-top: 8px;
          }
          .info {
            margin-top: 8px;
            -webkit-line-clamp: 1;
          }
          .bottom {
            margin-top: 8px;
          }
        }
      }
    }
  }
}
</style>
